package com.ruoyi.web.controller.api.massage;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.system.domain.PChatList;
import com.ruoyi.system.domain.PMessage;
import com.ruoyi.system.domain.vo.PChatDetailsVo;
import com.ruoyi.system.service.IPChatDetailsService;
import com.ruoyi.system.service.IPChatListService;
import com.ruoyi.system.service.IPMessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = {"前端-消息管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/messages")
public class MessagesApiController {

    private final IPMessageService iPMessageService;

    private final IPChatListService iIPChatListService;


    private final IPChatDetailsService iPChatDetailsService;

    @ApiOperation("查看未读消息数量")
    @GetMapping("/messageCount")
    public R<Long> messageCount(PChatDetailsVo pChatDetailsVo) {
        PChatList pChatList = iIPChatListService.chatInt(LoginHelper.getUserId(),pChatDetailsVo.getStatus());
        if(pChatList != null){
            return R.ok(pChatList.getUnread());
        }
        return R.ok("0");
    }

    @ApiOperation("查询服务消息")
    @GetMapping("/serviceMessage")
    public R<List<Map<String, Object>>> serviceMessage() {
        List<Map<String, Object>> pChatList = iIPChatListService.serviceMessage(LoginHelper.getUserId());
        return R.ok(pChatList);
    }

    @ApiOperation("查询文字代办")
    @GetMapping("/writtenAgent")
    public R<List<Map<String, Object>>> writtenAgent() {
        List<Map<String, Object>> pChatList = iIPChatListService.writtenAgent(LoginHelper.getUserId());
        return R.ok(pChatList);
    }

    @ApiOperation("查询电话代办")
    @GetMapping("/telephoneAgent")
    public R<List<Map<String, Object>>> telephoneAgent() {
        List<Map<String, Object>> pChatList = iIPChatListService.telephoneAgent(LoginHelper.getUserId());
        return R.ok(pChatList);
    }

    @ApiOperation("查询咨询订单")
    @GetMapping("/consultingOrder")
    public R<List<Map<String, Object>>> consultingOrder(PChatDetailsVo pChatDetailsVo) {
        List<Map<String, Object>> pChatList = iIPChatListService.consultingOrder(LoginHelper.getUserId(),pChatDetailsVo.getStatus());
        return R.ok(pChatList);
    }

    @ApiOperation("首页人数")
    @Anonymous
    @GetMapping("/indexPersonCount")
    public R<Long> indexPersonCount() {
        return R.ok(this.iPMessageService.indexPersonCount());
    }

    @ApiOperation("首页消息中心")
    @GetMapping("/indexMessageIndex")
    public TableDataInfo<PMessage> indexMessageIndex(@RequestParam List<String> secondType, PageQuery pageQuery) {
        return iPMessageService.indexMessageIndex(secondType, pageQuery);
    }

    @ApiOperation("发送消息")
    @PostMapping("/sendMessage")
    public R<Void> sendMessage(@RequestBody PMessage pMessage) {
        this.iPMessageService.sendMessage(pMessage);
        return R.ok("发送成功");
    }
}
